import { remove } from '@vue/shared'
import { defineStore } from 'pinia'
import type { HistoryState, NavigationFailure, RouteLocationNamedRaw, RouteLocationPathRaw, RouteLocationRaw, Router, RouteRecordRaw, RouterHistory } from 'vue-router'
import { createRouter, createWebHistory } from 'vue-router'
function getComponentName(data: any): string | undefined {
    if ('path' in data && data.path !== null) {
        let path = data.path as string
        const before = path.split('?')[0].split('/')
        return before[before.length - 1]
    } else if ('name' in data && data.name !== null) {
        return data.name as string
    } else {
        return undefined
    }
}
type HistoryData = {
    recordNames: string[],//历史路由数组
}
export const piniaKeepAlive = defineStore({
    id: 'keep_alive_id',
    state: () => (<HistoryData>{
        recordNames: ["TestRouter1Ac", "TestRouter31Fra","TestRouter32Fra"]
    }),
    actions: {

        push(componentName: string) {
            //将router的name转成component的name
            // const componentName = name.slice(1, name.length)
            //判断是否已存在
            if (this.recordNames.indexOf(componentName) <= -1) {
                this.recordNames.push(componentName)
                console.log("触发history push", this.recordNames);
            } else {
                console.log("触发history push 已存在不添加", componentName);
            }
        },
        remove(componentName: string) {

        },
        removeIndex(backNumber: number) {
            if (this.recordNames.length > backNumber) {
                this.recordNames = this.recordNames.slice(0, this.recordNames.length - backNumber)
            } else {
                this.recordNames = this.recordNames.slice(0, 1)
            }
            console.log("触发history removeIndex", this.recordNames);
        }
    },
    getters: {
        // getState(state): boolean {
        //     return state.mHasKeepAlive
        // }
    }
})

// export function createRouterHistory(): RouterHistory {
//     let historyRouter = createWebHistory(import.meta.env.BASE_URL)
//     const oldPush = historyRouter.push
//     const oldReplace = historyRouter.replace
//     const oldGo = historyRouter.go
//     historyRouter.push = (to: string, data?: HistoryState) => {
//         console.log("push", to, data);
//         piniaKeepAlive().push(to)
//         oldPush(to, data)
//     }
//     historyRouter.replace = (to: string, data?: HistoryState) => {
//         console.log("replace", to, data);
//         oldReplace(to, data)
//     }
//     historyRouter.go = (n: number) => {
//         piniaKeepAlive().removeIndex(-n)
//         console.log("go", -n);
//         oldGo(n)
//     }
//     return historyRouter
// }
// function getName(data: string): string | null {
//     return null 
// }



export function ycCreateRouter(constantRoutes: Readonly<RouteRecordRaw[]>): Router {
    let router = createRouter({
        history: createWebHistory(import.meta.env.BASE_URL),
        routes: constantRoutes,
    })
    const oldPush = router.push
    const oldReplace = router.replace
    const oldBack = router.back
    const oldGo = router.go

    router.push = (to: RouteLocationRaw): Promise<NavigationFailure | void | undefined> => {
        const componentName = getComponentName(to)
        if (componentName !== null || componentName !== undefined) {
            piniaKeepAlive().push(componentName!)
        }
        console.log("push", to);
        return oldPush(to)
    }
    router.replace = (to: RouteLocationRaw): Promise<NavigationFailure | void | undefined> => {
        console.log("replace", to);
        return oldReplace(to)
    }
    router.back = (): ReturnType<Router['go']> => {
        piniaKeepAlive().removeIndex(1)
        console.log("back");
        oldBack()
    }
    router.go = (delta: number) => {
        piniaKeepAlive().removeIndex(-delta)
        console.log("go", delta);
        oldGo(delta)
    }
    return router
} 
